﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SystemCenterSoa.DtoModel.SystemManager.ApplyGather;
using SystemCenterSoa.IRepository.SystemManager.ApplyGather;
using SystemCenterSoa.PoModel.SystemManager.ApplyGather;
using SystemCenterSoa.Repository.CommonLib;

namespace SystemCenterSoa.Repository.SystemManager.ApplyGather
{
    public class NotClockApplyRepository : BaseRepository, INotClockApplyRepository
    {
        public NotClockApplyRepository(SystemCenterDbSoaContext systemCenterDbSoaContext) : base(systemCenterDbSoaContext)
        {
        }
        public bool GetNotClockApplyAdd(NotClockApplyPo notClockApply)
        {
            _systemCenterDbSoaContext.NotClockApplys.Add(notClockApply);
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
        public List<NotClockApplyDto> GetNotClockApplyList(int deId, string nian, string yue, int pageIndex, int pageSize)
        {
            var query = from A in _systemCenterDbSoaContext.NotClockApplys
                        join B in _systemCenterDbSoaContext.DepartmentPo
                        on A.de_id equals B.deptId
                        select new NotClockApplyDto
                        {
                            wdk_id = A.wdk_id,
                            wdk_start_date = A.wdk_start_date,
                            wdk_end_date = A.wdk_end_date,
                            wdk_why = A.wdk_why,
                            de_id = A.de_id,
                            deptName = B.deptName,
                        };
            if (deId > 0)
            {
                query = query.Where(t => t.de_id == deId);
            }
            if (!string.IsNullOrWhiteSpace(nian))
            {
                query = query.Where(t => t.wdk_start_date.Year.ToString() == nian);
            }
            if (!string.IsNullOrWhiteSpace(yue))
            {
                query = query.Where(t => t.wdk_start_date.Month.ToString() == yue);
            }
            return query.ToList();
        }
        public NotClockApplyPo GetNotClockApplyFan(int id)
        {
            return _systemCenterDbSoaContext.NotClockApplys.Find(id);
        }
        public bool GetNotClockApplyUpdate(NotClockApplyPo notClockApply)
        {
            _systemCenterDbSoaContext.Entry(notClockApply).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
        public bool GetNotClockApplyDel(string ids)
        {
            var ArrId = ids.Split(',');
            foreach (var item in ArrId)
            {
                int id = Convert.ToInt32(item);
                _systemCenterDbSoaContext.NotClockApplys.Remove(_systemCenterDbSoaContext.NotClockApplys.Find(id));
            }
            return _systemCenterDbSoaContext.SaveChanges() > 0;
        }
    }
}
